package com.fzj.charging.order.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class MqttProducer {
    @Autowired
    private MqttClient mqttClient;

    /**
     * 通过MqttClient发送消息到EMQX
     * @param topic
     * @param message
     * @return
     * @throws Exception
     */
    public Boolean send(String topic,String message){
       Boolean success = false;
        try {
            mqttClient.publish(topic,message.getBytes(),0,false);
            success = true;
        } catch (MqttException e) {
            log.error("给EMQX发送消息失败，topic：{}。message：{}",topic,message,e);
        }
        return success;
    }
}
